package org.wangtian.youxueban.excel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wangtian.youxueban.excel.bo.DaliyPracticeBo;
import org.wangtian.youxueban.excel.util.DateUtil;
import org.wangtian.youxueban.excel.util.ImportExcelUntil;
import org.wangtian.youxueban.excel.dao.DaliyPracticeRepository;

import java.util.Date;
import java.util.List;

@Service
public class DpService {
    @Autowired
    private ImportExcelUntil importExcelUntil;
    @Autowired
    DaliyPracticeRepository daliyPracticeRepository;

    /**
     *
     */
    public void readAndSave(String from,String to){
        Date fromDate= DateUtil.toDate(from);
        Date toDate= DateUtil.toDate(to);
        while(true){
            long timeFromDate=fromDate.getTime()+24*60*60*1000;
            Date date=new Date(timeFromDate);
            String dd=DateUtil.getStringDate(date);
            this.save(importExcelUntil.readXlsx(dd));
            fromDate=date;
            if(fromDate.after(toDate)){
                break;
            }
        }

    }

    /**
     *
     */
    public void readAndSave(){
        this.save(importExcelUntil.readXlsx("2020-10-16"));
    }

    /**
     *
     * @param daliyPracticeBos
     */
    public void save(List<DaliyPracticeBo> daliyPracticeBos){
        daliyPracticeRepository.saveAll(daliyPracticeBos);
    }
}
